package com.wh.wisdomsite.xbox.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Vector;


/**
 * 功能描述：
 * @Package com.wh.wisdomsite.xbox.server
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class JndiManager {

    public static synchronized void close() {
        freeDll("sapjcorfc");
    }

    public static synchronized void unloadAllNativeLibs() {
        try {
            ClassLoader classLoader = JndiManager.class.getClassLoader();
            Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
            field.setAccessible(true);
            Vector<Object> libs = (Vector<Object>) field.get(classLoader);
            Iterator it = libs.iterator();
            while (it.hasNext()) {
                Object object = it.next();
                Method finalize = object.getClass().getDeclaredMethod("finalize");
                finalize.setAccessible(true);
                finalize.invoke(object);
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
        System.out.println("JNDI unloadAllNavtiveLibs...");
    }

    public static synchronized void unloadNativeLibs(String libName) {
        try {
            ClassLoader classLoader = JndiManager.class.getClassLoader();
            Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
            field.setAccessible(true);
            Vector<Object> libs = (Vector<Object>) field.get(classLoader);
            Iterator it = libs.iterator();
            while (it.hasNext()) {
                Object object = it.next();
                Field[] fs = object.getClass().getDeclaredFields();
                for (int k = 0; k < fs.length; k++) {
                    if (fs[k].getName().equals("name")) {
                        fs[k].setAccessible(true);
                        String dllPath = fs[k].get(object).toString();
                        if (dllPath.endsWith(libName)) {
                            Method finalize = object.getClass().getDeclaredMethod("finalize");
                            finalize.setAccessible(true);
                            finalize.invoke(object);
                        }
                    }
                }
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public static synchronized void freeDll(String dllName) {
        try {
            ClassLoader classLoader = JndiManager.class.getClassLoader();
            Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
            field.setAccessible(true);
            Vector<Object> libs = (Vector<Object>) field.get(classLoader);
            //System.out.println(libs);
            Iterator<Object> it = libs.iterator();
            Object o;
            while (it.hasNext()) {
                o = it.next();
                Field[] fs = o.getClass().getDeclaredFields();
                boolean hasInit = false;
                for (int k = 0; k < fs.length; k++) {
                    if (fs[k].getName().equals("name")) {

                        fs[k].setAccessible(true);
                        String dllPath = fs[k].get(o).toString();

                        //System.out.println(dllPath);
                        if (dllPath.contains(dllName)) {
                            hasInit = true;
                        }
                    }
                }
                if (hasInit) {
                    Method finalize = o.getClass().getDeclaredMethod(
                            "finalize", new Class[0]);
                    finalize.setAccessible(true);
                    finalize.invoke(o, new Object[0]);
                    it.remove();
                    libs.remove(o);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
